<template>
	<!-- 中捷/武威监狱版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.WarehouseManage.box')" />
		<!-- 查询结果列表 -->
		<zy-table :headList="headList" :dataList="dataList" border></zy-table>
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')"
			:leftDisabled="dataList.length>0?false:true" :rightDisabled="dataList.length>0?false:true"
			@rightBtn="submitBtn" @leftBtn="resetBtn">
		</bottomBtn>
		<!-- 提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.goodsDelivery')" type="dialog" @confirm="confirmBtn">
			{{$t('homepage.WarehouseManage.submitInfo')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getOutInstructionsMainByNo,
		findBoxByMaterialCode,
		getStorehouseDetailByCode,
		saveStorehouseOut
	} from "@/api/homepage.js"
	export default {
		components: {},
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('public.code'),
					key: 'barcode',
				}, {
					name: this.$t('public.productionNo'),
					key: 'productionTaskNo',
				}, {
					name: this.$t('public.surplusNum'),
					key: 'surplusNum',
				}],
				dataList: [], //表格数据
			}
		},
		onLoad() {
			//PDA扫描主页指示单号
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				findBoxByMaterialCode({
					billNo: e,
				}).then((res) => {
					console.log(res)
					if (res.code == 0) {
						if (this.dataList.findIndex(item => item.barcode === e) !== -1) {
							uni.showToast({
								title: this.$t('public.searchRepeat'),
								icon: "none",
								duration: 3000
							})
						} else {
							this.dataList.push(res.data)
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((err) => {
				console.log(err)
					uni.showToast({
						title: err.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 重置页面
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/ProductManagement/Outstorage/index"
				})
			},
			//底部按钮事件
			submitBtn() {
				this.$refs.popup.open()
			},
			// 二次确认提交
			confirmBtn: function() {
				saveStorehouseOut({
					billType: 5,
					dataArray: JSON.stringify(this.dataList)
				}).then((res) => {
					if (res.code == 0) {
						setTimeout(() => {
							this.resetBtn()
						}, 500)
					} else {
						this.$refs.popup.close()
					}
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				}).catch((err) => {
					uni.showToast({
						title: err.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.zy_btn_group {
		bottom: 0;
		position: fixed;
		width: 100%;

		.zy_btn {
			width: 50%;
		}
	}
</style>